﻿using System.Collections.Generic;

namespace Sidvall.CodeGenerator
{
	public class DomainObjectRelationPath
	{
		#region Business Members

		public System.Collections.ObjectModel.Collection<DomainObjectRelation> DomainObjectRelations { get; private set; }

		#region EndDomainObject

		public DomainObject EndDomainObject
		{
			get
			{
				if (this.DomainObjectRelations.Count == 0)
					return null;
				return this.DomainObjectRelations[this.DomainObjectRelations.Count - 1].RelatedObject;
			}
		}

		#endregion
		#region StartDomainObject

		public DomainObject StartDomainObject
		{
			get
			{
				if (this.DomainObjectRelations.Count == 0)
					return null;
				return this.DomainObjectRelations[0].RelatedObject;
			}
		}

		#endregion

		#region Copy

		public DomainObjectRelationPath Copy()
		{
			var item = new DomainObjectRelationPath();
			foreach (var domainObjectRelation in this.DomainObjectRelations)
				item.DomainObjectRelations.Add(domainObjectRelation);

			return item;
		}

		#endregion
		#region GetName

		public string GetName(string separator)
		{
			var sb = new System.Text.StringBuilder();
			foreach (var domainObjectRelation in this.DomainObjectRelations)
			{
				if (sb.Length > 0)
					sb.Append(separator);
				sb.Append(domainObjectRelation.Name);
			}
			return sb.ToString();
		}

		#endregion
		#region GetNameInDataStore

		public string GetNameInDataStore(string separator)
		{
			System.Text.StringBuilder sb;

			sb = new System.Text.StringBuilder();
			foreach (var domainObjectRelation in this.DomainObjectRelations)
			{
				if (sb.Length > 0)
					sb.Append(separator);
				sb.Append(domainObjectRelation.NameInDataStore);
			}

			return sb.ToString();
		}

		#endregion
		#region GetRelatedObjectTypeNames

		public string GetRelatedObjectTypeNames(string separator, string namePrefix)
		{
			var index = new HashSet<string>();
			var sb = new System.Text.StringBuilder();
			foreach (var domainObjectRelation in this.DomainObjectRelations)
			{
				if (index.Contains(domainObjectRelation.RelatedObject.TypeName))
					continue;
				index.Add(domainObjectRelation.RelatedObject.TypeName);
				if (sb.Length > 0)
					sb.Append(separator);
				sb.Append(namePrefix + domainObjectRelation.RelatedObject.TypeName);
			}
			return sb.ToString();
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			return GetName("");
		}

		#endregion

		#endregion
		#region Constructors

		public DomainObjectRelationPath()
		{
			this.DomainObjectRelations = new System.Collections.ObjectModel.Collection<DomainObjectRelation>();
		}

		#endregion
	}
}
